package com.bh.weapp.controller.sysuser.shipment;

import com.bh.common.enums.ShipmentStatus;
import com.bh.common.core.controller.BaseController;
import com.bh.common.core.domain.AjaxResult;
import com.bh.shipment.domain.JcShipment;
import com.bh.shipment.domain.JcVehicleArrive;
import com.bh.shipment.service.IJcShipmentService;
import com.bh.shipment.service.IJcVehicleArriveService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * jc vehicele到控制器
 *
 * @author shuangluxing@163.com
 * @date 2023/07/21
 */
@Api(tags = {"SYS运单抵达接口"})
@RestController
@RequestMapping("/api/sysuser/vehicleArrive")
public class JcVehiceleArriveApiController extends BaseController {


    @Autowired
    private IJcVehicleArriveService vehicleArriveService;
    @Autowired
    private IJcShipmentService shipmentService;
    /**
     * 车辆到站操作
     * @param jcShipmentId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("车辆到站操作")
    @PostMapping("/saveVehicleArrive/{jcShipmentIds}")
    public AjaxResult saveVehicleArrive(@PathVariable Long[] jcShipmentIds) {
        return toAjax(vehicleArriveService.saveVehicleArrive(jcShipmentIds));
    }

    /**
     * 车辆再次发运时间
     * @param vehicleArrive
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping(value = "/saveShippingTime")
    public AjaxResult saveShippingTime(JcVehicleArrive vehicleArrive) {
        if (vehicleArrive.getOperateTime() == null) {
            return AjaxResult.error("发运时间不能为空！");
        }
        JcShipment shipment = shipmentService.selectJcShipmentById(vehicleArrive.getJcShipmentId());
        if (shipment.getStatus() == ShipmentStatus.VEHICLE_ARRIVE.getValue()) {
            return toAjax(vehicleArriveService.saveShippingTime(vehicleArrive));
        } else {
            return AjaxResult.error("运单状态不正确！");
        }

    }

    @Transactional(rollbackFor = Exception.class)
    @PostMapping(value = "/saveArriveTime")
    public AjaxResult saveArriveTime(JcVehicleArrive vehicleArrive) {
        if (vehicleArrive.getArriveTime() == null) {
            return AjaxResult.error("运抵时间不能为空！");
        }
        return toAjax(vehicleArriveService.saveArriveTime(vehicleArrive));
    }

    @GetMapping(value = "/getVehiclePrint/{shipmentId}}")
    public AjaxResult getVehiclePrint(@PathVariable Long shipmentId) {
        return AjaxResult.success(vehicleArriveService.getVehiclePrint(shipmentId));
    }
}
